
***** Measure deterrence effects with synthetic controls

local StJoTreat = mofd(mdy(11,16,2004))
local StJoSettle = mofd(mdy(12,21,2007))

local WheatonTreat = mofd(mdy(10,19,2004))
local WheatonSettle = mofd(mdy(1,7,2010))

local ElCentroTreat = mofd(mdy(3,20,2006))
local ElCentroSettle = mofd(mdy(9,20,2010))

local OverlookTreat = mofd(mdy(4,25,2008))
local OverlookSettle = mofd(mdy(6,21,2012))

local MortonPlantTreat = mofd(mdy(8,27,2008)) 
local MortonPlantSettle = mofd(mdy(11,20,2012))

local ShandsTreat = mofd(mdy(8,30,2008))
local ShandsSettle = mofd(mdy(8,8,2013))

local chsTreat = mofd(mdy(1,7,2009))
local chsSettle= mofd(mdy(8,4,2014))

cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop

local dftlist "StJo Wheaton ElCentro Overlook MortonPlant Shands chs"

foreach dft of local dftlist {

	cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop

	** Keep only defendant 
	use ipop_controls_panel.dta, clear
	keep if prvdrnum == "`dft'"

	local treatment_period = ``dft'Treat'

	
	** Create flatline projection
	gen last12 = pmt_amt if `treatment_period' - dt <= 12 & `treatment_period' - dt > 0 
	egen flatline = mean(last12)
	replace flatline = . if dt < `treatment_period' - 12


	** Produce difference in series
	gen deterrence = flatline - pmt_amt if dt >= `treatment_period'


	** Time discount the difference at 10% 
	local r = 1.1^(1/12)

	gen deterrence_disc = deterrence*1/(`r'^(dt-`treatment_period'))
	replace deterrence_disc = . if dt-`treatment_period' >= 60
	count if !mi(deterrence_disc)
	assert(r(N)==60)

	*** Compute total
	preserve
	collapse (sum) deterrence_disc
	qui summarize deterrence_disc
	local deterrence_`dft' = r(min)
	restore

	*** Truncate graph at 5 years 
	drop if dt  - `treatment_period' >= 60
	summarize dt 
	local minperiod = r(min)
	local maxperiod = r(max)

	** Find max Y 
	summarize pmt_amt
	local ymax = 1.1*r(max)

	format dt %tmMon_YY
	twoway (line pmt_amt dt, lcolor(navy) lwidth(thick)) (line flatline dt, lwidth(thick) lcolor(maroon) lpattern(dash)) (rarea pmt_amt flatline dt if dt>= `treatment_period', color(gs5)), xline(`treatment_period', lcolor(maroon)) xline(``dft'Settle', lcolor(black)) legend(order(1 "`dft'" 2 "Flat Line Projection") width(100)) xscale(range(`minperiod' `maxperiod')) xlabel(468 492 516 ``dft'Treat' ``dft'Settle' 600) xtitle("Month") ytitle("$/Month") graphregion(color(white)) ylabel(#3, format(%13.0fc)) yscale(range(0 `ymax'))

	graph save "robustness/flatline/`dft'Overlay.gph", replace
}


cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/robustness/flatline

log using deterrence.log, replace
local dftlist "StJo Wheaton ElCentro Overlook MortonPlant Shands chs"
local total = 0 
foreach dft of local dftlist {
	disp "`dft' Deterrence:"
	disp `deterrence_`dft''
	local total = `total' + `deterrence_`dft''
}
disp `total'
log close 


** Fix CHS graph
cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/robustness/flatline
graph use "chsOverlay.gph"
gr_edit .yaxis1.plotregion.yscale.curmin = 100000000
* Manually edit labels
graph save "chsOverlay.gph", replace 


